/**
 * @file function_object.cpp
 * @author your name (you@domain.com)
 * @brief 学习C++中的伪函数：https://blog.csdn.net/Bruce_0712/article/details/55049142
 *                        http://c.biancheng.net/view/520.html
 *                        https://docs.microsoft.com/en-us/cpp/standard-library/function-objects-in-the-stl?view=msvc-170
 *
 *
 * @version 0.1
 * @date 2022-03-24
 * 
 * @copyright Copyright (c) 2022
 * 
 */
//函数对象就是重载了"()"操作符的对象，因为调用这样的对象的形式是:f(p1, p2)，很像函数，所以称为函数对象，或者伪函数
#include <map>
#include <string>
#include <iostream>
#include <vector>
class MyStringSort {
    // 这个是函数对象必须要重载的符号，同时其必须是const类型的方法，否则会报错:
    // error: no matching function for call to object of type 'const MyStringSort'
    public:
    bool operator()(const std::string &s1, const std::string & s2) const;
};

bool MyStringSort::operator()(const std::string &s1, const std::string & s2) const {
    return s1 > s2;
}

int main_function_object()
{
    using std::vector;
    using std::map;
    using std::string;
    using std::cout;
    map<string, vector<int> ,MyStringSort> myMap;
    vector<int> v1 {10};
    vector<int> v2 {20};
    vector<int> v3 {30};
    myMap.emplace("AAAA",v1);
    myMap.emplace("CCCC",v2);
    myMap.emplace("BBBB",v3);
    map<string, vector<int>>::iterator itr;
    for(itr = myMap.begin(); itr != myMap.end(); ++itr) {
        vector<int> v = itr->second;
        cout << itr->first << " : " << v.front() << std::endl;
    }
    return 0;
}

